package com.chkj.fbook.business.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.chkj.fbook.common.utils.PageUtils;
import com.chkj.fbook.common.utils.Query;
import com.chkj.fbook.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chkj.fbook.business.domain.RoomDO;
import com.chkj.fbook.business.domain.SeatDO;
import com.chkj.fbook.business.service.RoomService;
import com.chkj.fbook.business.service.SeatService;
import com.chkj.fbook.common.annotation.Log;
import com.chkj.fbook.common.annotation.Permission;

/**
 * 桌位/座位管理
 * 
 * @author shh_he
 * @email 980027164@qq.com
 * @date 2023-10-02 21:43:17
 */
 
@Controller
@RequestMapping("/business/seat")
public class SeatController {

	@Autowired
	private SeatService seatService;
	
	@Autowired
	private RoomService roomService;
	
	@GetMapping()
	@Permission(name = "business:seat:seat")
	public String Seat(){
	    return "business/seat/seat";
	}
	
	@Log("桌位/座位管理-去到列表页面")
	@ResponseBody
	@GetMapping("/list")
	@Permission(name = "business:seat:seat")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<SeatDO> seatList = seatService.list(query);
		int total = seatService.count(query);
		PageUtils pageUtils = new PageUtils(seatList, total);
		return pageUtils;
	}
	
	@Log("桌位/座位管理-去到我新增页面")
	@GetMapping("/add")
	@Permission(name = "business:seat:add")
	public String add(Map<String, Object> params,Model model){
		
		List<RoomDO> roomList = roomService.list(params);
		model.addAttribute("roomList", roomList);
		
	    return "business/seat/add";
	}

	@Log("桌位/座位管理-去到修改页面")
	@GetMapping("/edit/{id}")
	@Permission(name = "business:seat:edit")
	public String edit(@PathVariable("id") String id,Model model){
		SeatDO seat = seatService.get(id);
		model.addAttribute("seat", seat);
		
		List<RoomDO> roomList = roomService.list(new HashMap<String, Object>());
		model.addAttribute("roomList", roomList);
		
	    return "business/seat/edit";
	}
	
	/**
	 * 保存
	 */
	@Log("桌位/座位管理-保存操作")
	@ResponseBody
	@PostMapping("/save")
	@Permission(name = "business:seat:add")
	public R save(SeatDO seat){
		
		RoomDO roomDO = roomService.get(seat.getRoomId());
		if(roomDO==null) {
			return R.error("所选择房间不存在！");
		}
		seat.setRoomName(roomDO.getName());
		
		if(seat.getNum()==null) {
			return R.error("桌位/座位排序编号不能为空！");
		}
		
		if(seatService.save(seat)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@Log("桌位/座位管理-修改操作")
	@ResponseBody
	@RequestMapping("/update")
	@Permission(name = "business:seat:edit")
	public R update(SeatDO seat){
		
		RoomDO roomDO = roomService.get(seat.getRoomId());
		if(roomDO==null) {
			return R.error("所选择房间不存在！");
		}
		seat.setRoomName(roomDO.getName());
		
		if(seat.getNum()==null) {
			return R.error("桌位/座位排序编号不能为空！");
		}
		
		seatService.update(seat);
		return R.ok();
	}
	
	/**
	 * 上架下架操作
	 */
	@Log("桌位/座位管理-上架和下架操作")
	@PostMapping( "/resetState")
	@ResponseBody
	@Permission(name = "business:seat:edit")
	public R resetState(SeatDO seat){
		if(seatService.resetState(seat)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@Log("桌位/座位管理-删除操作")
	@PostMapping( "/remove")
	@ResponseBody
	@Permission(name = "business:seat:remove")
	public R remove( String id){
		if(seatService.remove(id)>0){
			return R.ok();
		}
		return R.error();
	}
	
}
